﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WpfApp.Commands;
using WpfApp.Models;

namespace WpfApp.ViewModels
{
    public class TestViewModel
    {

        public DelegateCommand CopyCmd { get; set; }

        public DelegateCommand CalculateKillCodeCmd { get; set; }


        public TestModel Model { get; set; }

        public List<LotteryModel> LotteryModels { get; set; }

        public TestViewModel()
        {
            this.Model = new TestModel();
            this.CopyCmd = new DelegateCommand();
            this.CopyCmd.ExecuteCommand = new Action<object>(this.Model.Copy);
            this.CalculateKillCodeCmd = new DelegateCommand();
            this.CalculateKillCodeCmd.ExecuteCommand = new Action<object>(this.Model.CalculateKillCode);
            this.GetLotteryModels();
        }

        private void GetLotteryModels()
        {
            this.LotteryModels = new List<LotteryModel>();

            XDocument doc = XDocument.Load(@"E:\winapp\wpf\WpfApp\WpfApp\Data\TDHistory.xml");
            XDocument docK = XDocument.Load(@"E:\winapp\wpf\WpfApp\WpfApp\Data\KillHistory.xml");
            int index = 0;

            var test = from k in docK.Descendants("Item")
                                         //.Where(w => w.Element("IssueNumber").Value.ToString() == t.Attribute("IssueNumber").Value.ToString())
                       select new KillCodeModel
                       {
                           KillOneBits = k.Attribute("KillOneBits").Value.ToString(),
                       };

           
            var text = from t in doc.Descendants("Issue")                    //定位到节点 
                       //.Where(w => w.Element("to").Value.Contains('@'))   //若要筛选就用上这个语句 
                       select new LotteryModel
                       {
                           Id = ++index,
                           IssueNumber = t.Attribute("IssueNumber").Value.ToString(),
                           LotteryNumber = t.Attribute("LotteryNumber").Value.ToString(),
                           TestingNumber = t.Attribute("TestingNumber").Value.ToString(),
                           IssueDate = Convert.ToDateTime(t.Attribute("IssueDate").Value.ToString()),
                           KillCode = (from k in docK.Descendants("Item")
                                       .Where(w => w.Attribute("IssueNumber").Value.ToString() == t.Attribute("IssueNumber").Value.ToString())
                                       select new KillCodeModel
                                       {
                                           IssueNumber = k.Attribute("IssueNumber").Value.ToString(),
                                           KillOneCode = k.Attribute("KillOneCode").Value.ToString(),
                                           KillOneHundred = k.Attribute("KillOneHundred").Value.ToString(),
                                           KillOneTen = k.Attribute("KillOneTen").Value.ToString(),                                        
                                           KillOneBits = k.Attribute("KillOneBits").Value.ToString(),

                                       }).FirstOrDefault<KillCodeModel>()
                       };

            this.LotteryModels = text.ToList<LotteryModel>(); //Take<LotteryModel>(200).

        }
    }
}
